package com.jyxd.web.controller.dictionary;

import com.jyxd.web.data.dictionary.RespiratorBoxDictonary;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.data.user.User;
import com.jyxd.web.service.dictionary.RespiratorBoxDictonaryService;
import com.jyxd.web.service.dictionary.RespiratorDictonaryService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.JsonArrayValueProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 呼吸机盒子字典
 */
@Api(tags = "呼吸机盒子字典相关接口")
@Controller
@RequestMapping(value = "/respiratorBoxDictonary")
public class RespiratorBoxDictonaryController {

    private static Logger logger = LoggerFactory.getLogger(RespiratorDictonaryService.class);

    @Autowired
    private RespiratorBoxDictonaryService respiratorBoxDictonaryService;

    /**
     * 增加一条呼吸机字典
     *
     * @return
     */
    @ApiOperation("增加一条呼吸机盒子字典记录")
    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult insert(@RequestBody RespiratorBoxDictonary respiratorBoxDictonary,HttpSession session) {
        User user=(User)session.getAttribute("user");
        if(user!=null && StringUtils.isNotEmpty(user.getTenantId())){
            String tenantId = user.getTenantId();
            if(StringUtils.isNotEmpty(tenantId)){
                respiratorBoxDictonary.setTenantId(tenantId);
            }
        }
        return respiratorBoxDictonaryService.saveOrUpdate(respiratorBoxDictonary);
    }

    /**
     * 更新呼吸机字典
     *
     * @param
     * @return
     */
    @ApiOperation("更新呼吸机盒子字典")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult update(@RequestBody(required = false) RespiratorBoxDictonary respiratorBoxDictonary,HttpSession session) {
        User user=(User)session.getAttribute("user");
        if(user!=null && StringUtils.isNotEmpty(user.getTenantId())){
            String tenantId = user.getTenantId();
            if(StringUtils.isNotEmpty(tenantId)){
                respiratorBoxDictonary.setTenantId(tenantId);
            }
        }
        return respiratorBoxDictonaryService.saveOrUpdate(respiratorBoxDictonary);
    }

    /**
     * 删除呼吸机字典
     *
     * @param map
     * @return
     */
    @ApiOperation("删除呼吸机盒子字典记录")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public String delete(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "系统开小差了，请稍后再试。");
        if (map.containsKey("id")) {
            int i = respiratorBoxDictonaryService.deleteById(map.get("id").toString());
            if (i == 1) {
                json.put("msg", "删除成功");
            } else {
                json.put("msg", "删除失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据主键id查询呼吸机盒子字典
     *
     * @param map
     * @return
     */
    @ApiOperation("根据主键id查询呼吸机盒子字典记录")
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map != null && map.containsKey("id")) {
            RespiratorBoxDictonary respiratorBoxDictonary = respiratorBoxDictonaryService.queryData(map.get("id").toString());
            if (respiratorBoxDictonary != null) {
                json.put("msg", "查询成功");
                json.put("data", JSONObject.fromObject(respiratorBoxDictonary));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据条件分页查询呼吸机盒子字典记录列表（也可以不分页）
     *
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询呼吸机盒子字典记录列表（也可以不分页）")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestBody(required = false) Map<String, Object> map, HttpSession session) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map == null) map = new HashMap<>();
        User user=(User)session.getAttribute("user");
        if(user!=null && StringUtils.isNotEmpty(user.getTenantId())){
            String tenantId = user.getTenantId();
            if(StringUtils.isNotEmpty(tenantId)){
                map.put("tenantId",tenantId);
            }
        }
        if (map != null && map.containsKey("start")) {
            int totalCount = respiratorBoxDictonaryService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }

        List<RespiratorBoxDictonary> list = respiratorBoxDictonaryService.queryList(map);
        if (list != null && list.size() > 0) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(Date.class, new JsonArrayValueProcessor());
            json.put("data", JSONArray.fromObject(list,jsonConfig));
            json.put("msg", "查询成功");
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 呼吸机绑定盒子
     *
     * @param map
     * @return
     */
    @ApiOperation("呼吸机绑定盒子")
    @RequestMapping(value = "/bindBox", method = RequestMethod.POST)
    @ResponseBody
    public String bindBox(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        String boxNumbers = (String) map.get("boxNumbers");
        String respiratorId = (String) map.get("respiratorId");
        //清空绑定呼吸机id的盒子
        respiratorBoxDictonaryService.cleanByRespiratorId(respiratorId);
        //更新盒子数据
        respiratorBoxDictonaryService.updateByRespiratorId(respiratorId, boxNumbers);
        json.put("data", null);
        json.put("msg", "绑定成功");
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
